define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'acitivity/cate/index' + location.search,
                    add_url: 'acitivity/cate/add',
                    edit_url: 'acitivity/cate/edit',
                    del_url: 'acitivity/cate/del',
                    table: 'acitivity_cate',
                }
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#current_img").text(data.current.avatar);
                $("#current_phone").text(data.current.mobile);
                $("#current_name").text(data.current.name);
                $("#current_time").text(data.current.createtime);
                $("#username").text(data.current.username);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                escape: false,
                columns: [
                    [
                        //{checkbox: true},
                        {
                            field: 'id',
                            title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
                            operate: false,
                            formatter: Controller.api.formatter.subnode
                        },
                        {field: 'id', title: __('单位编号'), operate:false},
                        {field: 'name', title: __('Name'), align: 'left', operate:'LIKE'},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            // formatter: Controller.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('活动管理'),
                                    text: __('活动管理'),
                                    classname: 'btn btn-xs btn-info btn-addtabs btn-ajax',
                                    icon: 'fa fa-folder-o',
                                    url: 'acitivity/cate/detail'+ location.search
                                }
                            ]

                        }
                    ]
                ],
                pagination: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //当内容渲染完成后
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                //默认隐藏所有子节点
                //$("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
                $(".btn-node-sub.disabled").closest("tr").hide();
                $(".btn-node-sub.btn-hidden").closest("tr").hide();

                //显示隐藏子节点
                $(".btn-node-sub").off("click").on("click", function (e) {
                    var status = $(this).data("shown") ? true : false;
                    $("a.btn[data-pid='" + $(this).data("id") + "']").each(function (e) {
                        $(this).closest("tr").toggle(!status);
                    });
                    if(status){
                        $(this).children("i").removeClass('fa-minus').addClass('fa-plus');
                    }else{
                        $(this).children("i").removeClass('fa-plus').addClass('fa-minus');
                    }

                    $(this).data("shown", !status);
                    return false;
                });
                //点击切换/排序/删除操作后刷新左侧菜单
                $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", function (data, ret) {
                    Fast.api.refreshmenu();
                    return false;
                });

            });
            //批量删除后的回调
            $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                Fast.api.refreshmenu();
            });

            //展开隐藏全部
            $(document.body).on("click", ".btn-toggle-all", function (e) {
                var that = this;
                var show = $("i", that).hasClass("fa-plus");
                $("i", that).toggleClass("fa-plus", !show);
                $("i", that).toggleClass("fa-minus", show);
                $(".btn-node-sub.disabled").closest("tr").toggle(show);
                $(".btn-node-sub").data("shown", show);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Table.api.init({
                extend: {
                    chapter_url: 'acitivity/cate/detail'+ location.search,
                    table: 'acitivity_cate_detail'
                }
            });
            var table1 = $('#table1');
            table1.on('post-common-search.bs.table', function (event, table) {
                $(".commonsearch-table").find('.btn-success').html('搜索');
                $(".commonsearch-table").find('.btn-default').html('刷新');
                $(".columns-right").hide();
                $('.form-commonsearch').attr('autocomplete', 'off');
            });
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.chapter_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate:'LIKE'},
                        {field: 'status', title: __('Status'), operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'user.nickname', title: __('User_name'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'image', title: __('Image'), operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'itemedit',
                                    title: __('编辑'),
                                    classname: 'btn btn-xs btn-success btn-click',
                                    icon: 'fa fa-pencil',
                                    // url: 'user/userinput/useredit'+ location.search,
                                    click: function (data,row) {
                                        edituser(row.id);
                                        // Layer.alert("点击按钮执行的事件");
                                    }
                                },
                                {
                                    name: 'itemdel',
                                    title: __('Delete'),
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: 'fa fa-trash',
                                    click: function (data,row) {
                                        confirmf(row.id);
                                        // Layer.alert("点击按钮执行的事件");
                                    }
                                }
                            ]

                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                search:false,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                    $(".columns-right").hide();
                }
            });
            $(".btn-close").on('click',function(){
                top.window.$("ul.nav-addtabs li.active").find(".fa-remove").trigger("click");
            });
            // 为表格绑定事件
            Table.api.bindevent(table1);
            var confirmf = function(id){
                Layer.confirm("确定删除该用户吗？",
                    {
                        title: '删除用户',
                        btn: ["确定", "取消"],
                        yes: function (index, layero) {
                            submitExportForm(id,index, layero);
                            return false;
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                            return false;
                        },
                    });
            }
            var submitExportForm = function (id,index, layero) {
                var data = {
                    ids: id
                };
                Fast.api.ajax({
                    url: Fast.api.fixurl('acitivity/cate/itemdel'),
                    data: data,
                    pk: 'id'
                }, function (res) {
                    Layer.closeAll();
                    Fast.api.close(layero.selector);
                    $(".btn-refresh").trigger("click");
                });
            };
            var edituser = function (id) {
                console.log('id',id)
                Fast.api.open('acitivity/cate/itemedit?ids='+id, '编辑', {
                    area:["100%","100%"],
                    callback:function(){
                        Fast.api.close();
                        $(".btn-refresh").trigger('click');//刷新当前页面的数据
                    }
                })
            }
        },
        itemadd: function () {
            Controller.api.bindevent();
        },
        itemedit: function () {
            Controller.api.bindevent();
        },
        itemdel: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs btn-success '+ (row.level == 3? 'btn-hidden' : '') + ' btn-node-sub">'+ (row.level == 3? '' : '<i class="fa fa-plus"></i>')+'</a>';
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

            }
        }

    };
    return Controller;
});